package cn.com.twoke.application.game.mario.game.entity.enemy;


import cn.com.twoke.application.game.mario.core.ecs.world.GameWorld;
import cn.com.twoke.application.game.mario.core.ecs.component.ColliderComponent;
import cn.com.twoke.application.game.mario.core.ecs.component.DataComponent;
import cn.com.twoke.application.game.mario.core.ecs.component.PositionComponent;
import cn.com.twoke.application.game.mario.core.ecs.component.RigidbodyComponent;
import cn.com.twoke.application.game.mario.core.ecs.component.render.animation.AnimationRenderComponent;
import cn.com.twoke.application.game.mario.core.ecs.component.render.group.GroupRenderComponent;
import cn.com.twoke.application.game.mario.core.ecs.entity.Entity;
import cn.com.twoke.application.game.mario.game.component.AutoMoveComponent;
import cn.com.twoke.application.game.mario.game.entity.AbstractEntityBuilder;
import cn.com.twoke.application.game.mario.game.util.TextureUtils;
import cn.com.twoke.application.game.mario.game.config.Tiles;
import cn.hutool.core.lang.Opt;

import java.awt.*;
import java.awt.image.BufferedImage;

/**
 * 敌人构建器
 */
public class EnemyBuilder extends AbstractEntityBuilder<EnemyBuilder> {
    static BufferedImage enemies;

    static {
        enemies = TextureUtils.loadImage("texture/enemies.png");
    }

    private final EnemyManager enemyManager;
    private EnemyType type = EnemyType.NORMAL_1;
    private String name = "Enemy";
    private boolean autoMoving = false;



    public EnemyBuilder(GameWorld world, EnemyManager enemyManager) {
        super(world);
        this.enemyManager = enemyManager;
    }

    public EnemyBuilder type(EnemyType type) {
        this.type = type;
        return this;
    }

    public EnemyBuilder  name(String name) {
        this.name = name;
        return this;
    }

    public EnemyBuilder autoMoving(boolean autoMoving) {
        this.autoMoving = autoMoving;
        return this;
    }


    @Override
    public Entity build() {
        Entity entity = this.world.createEntity();
        enemyManager.register(entity); // 注册到敌人管理器中
        // 设置位置
        PositionComponent positionComponent = new PositionComponent(startX, startY);
        DataComponent dataComponent = new DataComponent();
        dataComponent.set("type", type); // 设置敌人类型
        dataComponent.set("name", name); // 单独设置名称
        // 设置碰撞体
        ColliderComponent colliderComponent = new ColliderComponent(() -> new Rectangle(
                (int) positionComponent.x,
                (int) positionComponent.y,
                Tiles.widthOf(1),
                Tiles.heightOf(1)
        ));

        entity.addComponent(dataComponent); // 数据组件
        entity.addComponent(positionComponent); // 位置组件
        entity.addComponent(colliderComponent); // 碰撞体组件
        if (autoMoving) {
            entity.addComponent(new RigidbodyComponent()); // 刚体
            entity.addComponent(AutoMoveComponent.builder()
                    .moveSpeed(0.5f)
                    .build()); // 自动移动
        }
        doEnemyAnimationTexture(entity, dataComponent); // 处理敌人的动画纹理处理

        // 扩展操作
        Opt.ofNullable(extend).ifPresent(extend -> extend.accept(entity));
        return entity;
    }

    private void doEnemyAnimationTexture(Entity entity, DataComponent dataComponent) {
        GroupRenderComponent groupRenderComponent = GroupRenderComponent.builder().build();
        entity.addComponent(groupRenderComponent);
        AutoMoveComponent autoMove = entity.getComponent(AutoMoveComponent.class);

        if (dataComponent.get("type") == EnemyType.NORMAL_1) {
            groupRenderComponent.add(AnimationRenderComponent.builder()
                            .isShow(() -> true)
                            .animationTickCount(2)
                            .animationCostTime(100)
                            .animationTickTextureFun(idx -> enemies.getSubimage(idx * 16,0,16,16))
                    .build());
        }

//        // 名字
//        if (StrUtil.isNotBlank(dataComponent.getString("name"))) {
//            groupRenderComponent.add(TextRenderComponent.builder()
//                    .text(dataComponent.getString("name"))
//                            .color(Color.WHITE)
//                    .offsetY(-5)
//                    .build());
//        }
    }
}
